import { Application } from 'egg';

export default (app: Application) => {
  const { controller, router } = app;
  //controller是控制器对象  home就是HomeController类的一个实例
  router.get('/', controller.home.index);
  //1参数是指鉴权的方法 
  const localStrategy = app.passport.authenticate('local', {
    successRedirect: '/server/api/loginCallback',//不管鉴权成功和失败都会重定向到/api/loginCallback
    failureRedirect: '/server/api/loginCallback',
  });
  //localStrategy功能先从请求中获取到用户和密码，然后把它们传入我们自己写的callback,
  //鉴权成功后，会把用户对象传给done,done里面会把用户放入session会话，并重定向到/api/loginCallback
  router.post('/api/login/account', localStrategy);
  const auth = app.middleware.auth();
  router.post('/api/user/add', auth, controller.user.addUser);
  router.get('/api/loginCallback', controller.user.loginCallback);
};
